using System.Collections.Generic;
using UnityEngine;
using QFramework;

namespace XCZProject
{
	/// <summary>
	/// 篮球管理器，控制篮球的生成
	/// </summary>
	public partial class BasketBallAblity : ViewController
	{
		private List<Ball> mBalls = new List<Ball>();
		
		void Start()
		{
			// Code Here

			// 每当篮球数量改变的时候，都会触发事件，判断总篮球数是否大于记录的篮球数，不足则创建
			// Global.BasketBallCount.RegisterWithInitValue(count =>
			// {
			// 	if (count>mBalls.Count)
			// 	{
			// 		// 创建篮球
			// 		mBalls.Add(Ball.Instantiate()
			// 			.SyncPosition2DFrom(this)
			// 			.Show()
			// 		);
			// 	}
			// }).UnRegisterWhenGameObjectDestroyed(gameObject);
			Ball.Hide();

			// 这是一个组合触发事件，类似BasketBallCount.register+SimpleFlyCount.register
			Global.BasketBallCount.Or(Global.SimpleFlyCount).Register(() =>
			{
				CreateBalls();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			CreateBalls();
		}

		/// <summary>
		/// 创建篮球
		/// </summary>
		void CreateBalls()
		{
			var ballCount2Creat = Global.BasketBallCount.Value + Global.SimpleFlyCount.Value - mBalls.Count;

			//print(Global.BasketBallCount.Value+":"+Global.SimpleFlyCount.Value+":"+mBalls.Count);
			for (var i = 0; i < ballCount2Creat; i++)
			{
				//print("创建篮球:"+i);
				CreateBall();
			}
			print("创建完成，现在共有篮球数量："+mBalls);
		}

		/// <summary>
		/// 创建篮球
		/// </summary>
		void CreateBall()
		{
			// 创建篮球
			mBalls.Add(Ball.Instantiate()
				.SyncPosition2DFrom(this)
				.Show()
			);
		}
	}
}
